﻿using MES.Desktop.Entities;
using MES.Desktop.IDataAccess;
using MES.Desktop.IService;
using MES.Desktop.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MES.Desktop.Service
{
    public class UserService : ServiceBase, IUserService
    {
        IUserAccess _userAccess;
        public UserService(IUserAccess userAccess)
        {
            _userAccess = userAccess;
        }

        public UserEntity Login(string username, string password)
        {
            string json = _userAccess.Login(username, password);
            
            return this.GetResult<UserEntity>(json);
        }

        public bool UpdatePassword(int id, string opd, string npd)
        {
            string json = _userAccess.UpdatePassword(id, opd, npd);
            return this.GetResult<bool>(json);
        }


        public PageEntity<UserEntity[]> GetUsers(string key, string[] ds, string[] ps, int index, int size)
        {
            UserConditionEntity entity = new UserConditionEntity()
            {
                SearchKey = string.IsNullOrEmpty(key) ? "none" : key,
                DepartIds = ds,
                PostIds = ps
            };
            string json = JsonUtil.Serializer(entity);
            json = _userAccess.GetUsers(json, index, size);
            return this.GetResult<PageEntity<UserEntity[]>>(json);
        }

        public int UpdateUser(UserEntity entity)
        {
            string json = JsonUtil.Serializer(entity);
            json = _userAccess.UpdateUser(json);

            return this.GetResult<int>(json);
        }

        public int DeleteUser(int[] ids)
        {
            string json = JsonUtil.Serializer(ids);
            string result = _userAccess.DeleteUser(json);
            return this.GetResult<int>(result);
        }

        public bool LockUser(int id, int status)
        {
            string json = _userAccess.LockUser(id, status);
            return this.GetResult<bool>(json);
        }

        public bool CheckUserName(string username, int id)
        {
            string json = _userAccess.CheckUserName(username, id);
            return this.GetResult<bool>(json);
        }

        public int SaveUserRoles(RoleUserEntity[] roleUsers)
        {
            string json = JsonUtil.Serializer(roleUsers);
            json = _userAccess.SaveUserRoles(json);
            return this.GetResult<int>(json);
        }

        public int ResetPassword(int id)
        {
            string json = _userAccess.ResetPassword(id);
            return this.GetResult<int>(json);
        }

        public UserEntity[] GetUsersByIds(int[] ids)
        {
            string json = JsonUtil.Serializer(ids);
            json = _userAccess.GetUsersByIds(json);
            return this.GetResult<UserEntity[]>(json);
        }
    }
}

